package ca.lscs.web.utils;

import javax.servlet.http.HttpSession;

import ca.lscs.model.AppUser;
import ca.lscs.web.InvalidSessionException;

/**
 * @author Steve Pennington
 *
 */
public class SessionUtils {
	public static final String USER_SESSION_KEY = "USER_SESSION";
	
	public static AppUser getUserSession(HttpSession session) throws InvalidSessionException {
		Object obj = session.getAttribute(USER_SESSION_KEY);
		if(obj instanceof AppUser) {
			return (AppUser)obj;
		}
		
		session.removeAttribute(USER_SESSION_KEY);
		throw new InvalidSessionException();
	}
	
	public static void putUserSession(HttpSession session, AppUser user) {
		session.setAttribute(USER_SESSION_KEY, user);
	}
	
	public static void clearUserSession(HttpSession session) {
		session.removeAttribute(USER_SESSION_KEY);
		session.invalidate();
	}
}
